<template>
  <div>
    <div class="supplier" v-show="!menuAddShow">
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button
          v-hasPermi="['system:menu:query']"
          class="btn search"
          @click="handleQuery"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-hasPermi="['system:menu:add']"
          class="btn search"
          @click="
            menuAddShow = true;
            pageType = 'add';
          "
          ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
          >新增</el-button
        >
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item">
          <el-input
            v-model="queryParams.menuName"
            placeholder="请输入菜单名称"
            clearable
            class="search-input"
            @keyup.enter="handleQuery"
          />
        </div>
        <div class="search-item">
          <el-select
            class="search-input"
            v-model="queryParams.status"
            placeholder="菜单状态"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        border
        v-if="refreshTable"
        v-loading="loading"
        :data="menuList"
        row-key="menuId"
        :default-expand-all="isExpandAll"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          prop="menuName"
          label="菜单名称"
          :show-overflow-tooltip="true"
          width="160"
        ></el-table-column>
        <el-table-column prop="icon" label="图标" align="center" width="100">
          <template #default="scope">
            <svg-icon :icon-class="scope.row.icon" />
          </template>
        </el-table-column>
        <el-table-column
          prop="orderNum"
          label="排序"
          width="60"
        ></el-table-column>
        <el-table-column
          prop="perms"
          label="权限标识"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="component"
          label="组件路径"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          width="160"
          prop="createTime"
        >
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <!-- 操作模式 -->
        <el-table-column
          label="操作"
          :show-overflow-tooltip="true"
          width="140px"
          align="center"
        >
          <template #default="{ row }">
            <div class="table-operate">
              <el-icon
                v-hasPermi="['system:menu:add']"
                title="新增子菜单"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('add', row)"
              >
                <DocumentAdd />
              </el-icon>
              <el-icon
                v-hasPermi="['system:menu:edit']"
                title="编辑"
                color="#FFB700"
                class="table-icon"
                :size="20"
                @click="handleOperate('edit', row)"
              >
                <Edit />
              </el-icon>
              <el-icon
                v-hasPermi="['system:menu:remove']"
                title="删除"
                color="#E33838"
                class="table-icon"
                :size="20"
                @click="handleOperate('delete', row)"
              >
                <Delete />
              </el-icon>
            </div> </template
        ></el-table-column>
      </el-table>
    </div>
    <MenuAdd
      v-show="menuAddShow"
      ref="menuAddRef"
      v-model:menuAddShow="menuAddShow"
      @refreshList="handleQuery"
      :pageType="pageType"
    />
  </div>
</template>

<script setup name="Menu">
import MenuAdd from "./menuAdd";
import {
  addMenu,
  delMenu,
  getMenu,
  listMenu,
  updateMenu,
} from "@/api/system/menu";
import SvgIcon from "@/components/SvgIcon";
import { ElMessage, ElMessageBox } from "element-plus";
import IconSelect from "@/components/IconSelect";
import { ClickOutside as vClickOutside } from "element-plus";

const { proxy } = getCurrentInstance();
const { sys_show_hide, sys_normal_disable } = proxy.useDict(
  "sys_show_hide",
  "sys_normal_disable"
);

const menuList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const menuOptions = ref([]);
const isExpandAll = ref(false);
const refreshTable = ref(true);
const showChooseIcon = ref(false);
const menuAddShow = ref(false);
const iconSelectRef = ref(null);
const menuAddRef = ref(null);
const pageType = ref("");

const data = reactive({
  form: {},
  queryParams: {
    menuName: undefined,
    visible: undefined,
  },
  rules: {
    menuName: [
      { required: true, message: "菜单名称不能为空", trigger: "blur" },
    ],
    orderNum: [
      { required: true, message: "菜单顺序不能为空", trigger: "blur" },
    ],
    path: [{ required: true, message: "路由地址不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询菜单列表 */
function getList() {
  loading.value = true;
  listMenu(queryParams.value).then((response) => {
    menuList.value = proxy.handleTree(response.data, "menuId");
    loading.value = false;
  });
}
/** 查询菜单下拉树结构 */
function getTreeselect() {
  menuOptions.value = [];
  listMenu().then((response) => {
    const menu = { menuId: 0, menuName: "主类目", children: [] };
    menu.children = proxy.handleTree(response.data, "menuId");
    menuOptions.value.push(menu);
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    menuId: undefined,
    parentId: 0,
    menuName: undefined,
    icon: undefined,
    menuType: "M",
    orderNum: undefined,
    isFrame: "1",
    isCache: "0",
    visible: "0",
    status: "0",
  };
  proxy.resetForm("menuRef");
}

const handleOperate = async (type, row) => {
  switch (type) {
    case "add":
      menuAddShow.value = true;
      pageType.value = "add";
      nextTick(() => {
        menuAddRef.value.handleParentId(row.menuId);
      });
      break;
    case "edit":
      menuAddShow.value = true;
      pageType.value = "edit";
      nextTick(() => {
        menuAddRef.value.handleDetailInfo(row.menuId);
      });
      break;
    case "delete":
      ElMessageBox.confirm("此操作将删除该菜单，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
        type: "warning",
      })
        .then(async () => {
          const res = await delMenu(row.menuId);
          if (res.code === 200) {
            ElMessage.success(res.msg);
            handleQuery();
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
      break;

    default:
      break;
  }
};

/** 展示下拉图标 */
function showSelectIcon() {
  iconSelectRef.value.reset();
  showChooseIcon.value = true;
}
/** 选择图标 */
function selected(name) {
  form.value.icon = name;
  showChooseIcon.value = false;
}
/** 图标外层点击隐藏下拉列表 */
function hideSelectIcon(event) {
  var elem =
    event.relatedTarget ||
    event.srcElement ||
    event.target ||
    event.currentTarget;
  var className = elem.className;
  if (className !== "el-input__inner") {
    showChooseIcon.value = false;
  }
}
/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.menuId) {
    form.value.parentId = row.menuId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加菜单";
}
/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}
/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  getMenu(row.menuId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改菜单";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["menuRef"].validate((valid) => {
    if (valid) {
      if (form.value.menuId != undefined) {
        updateMenu(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMenu(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.menuName + '"的数据项?')
    .then(function () {
      return delMenu(row.menuId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
<style>
.confirmBtnClass {
  color: #fff;
  width: 90px;
  height: 34px;
  background: #007cef;
}
.confirmBtnClass:hover,
.confirmBtnClass:active,
.confirmBtnClass:focus {
  color: #fff;
  background: #007cef;
}
.cancelBtnClass {
  border: 1px solid #999999;
  color: #333333;
  width: 90px;
  height: 34px;
  background: #f3f3f3;
}
.cancelBtnClass:hover,
.cancelBtnClass:active,
.cancelBtnClass:focus {
  border: 1px solid #999999;
  color: #333333;
  background: #f3f3f3;
}
</style>
<style lang="scss" scoped>
:deep(.table-header) {
  background-color: #f8faff !important;
}

.supplier {
  padding: 20px 20px;
  width: 100%;
  height: 100%;
  .search {
    display: flex;
    margin-bottom: 20px;
    .search-item {
      margin-right: 20px;
    }
    .search-input {
      width: 200px;
      height: 34px;
    }
  }
  /* .search-button {
    position: fixed;
    top: 110px;
    right: 60px;
    display: flex;
    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 90px;
      height: 34px;
      padding: 10px;
      // margin-right: 20px;
      font-size: 14px;
      border-radius: 4px;
      user-select: none;
      cursor: pointer;
    }
    .btn.search {
      color: #fff;
      background: rgba(0, 124, 239, 1);
      &:hover {
        background: rgba(0, 124, 239, 0.85);
      }
    }
    .btn.add {
      border: none;
      color: #fff;
      background: rgba(255, 159, 0, 1);
      &:hover {
        background: rgba(255, 159, 0, 0.85);
      }
    }
    .btn.reset {
      color: rgba(43, 193, 123, 1);
      background: rgba(43, 193, 123, 0.05);
      border: 1px solid rgba(43, 193, 123, 1);
      &:hover {
        background: rgba(43, 193, 123, 0.2);
      }
    }
    .icon {
      margin-right: 6px;
    }
  } */

  .pagination {
    margin-top: 20px;
  }
  :deep(.el-table) {
    th.el-table__cell {
      font-size: 14px;
      font-weight: 400;
      height: 40px;
      color: #333333;
      background: #f8faff !important;
    }
    // 表格高度
    .el-table__cell {
      padding: 0;
      height: 40px;
    }
  }
  :deep(.el-table--large .cell) {
    padding: 0 20px 0 20px !important;
  }
}
</style>
